package com.cngphone.AnFetion;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class FetionHelper {
	public static String Byte2HexString(byte[] value) {
        String hexString = "";
        for (int i = 0; i < value.length; i++) {
        	if(Integer.toHexString(value[i] & 0xFF).length() == 1)
        		hexString += "0";
        	hexString += Integer.toHexString(value[i] & 0xFF).toUpperCase();
        }
        return hexString;
    }
	public static byte[] HexString2Bytes(String src)
	{
	    int len = (src.length() / 2);
	    byte[] result = new byte[len];
	    char[] achar = src.toCharArray();
	    for (int i = 0; i < len; i++) {
	     int pos = i * 2;
	     result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
	    }
	    return result;
	}

	private static byte toByte(char c) {
	    byte b = (byte) "0123456789ABCDEF".indexOf(c);
	    return b;
	}

	public static String HashPassword(String password,byte[] salt) throws NoSuchAlgorithmException, UnsupportedEncodingException
	{
		MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
		byte[] buffer = sha1.digest(password.getBytes("UTF8"));
		byte[] tmp = new byte[buffer.length + salt.length];
		System.arraycopy(salt,0, tmp, 0, salt.length);
		System.arraycopy(buffer,0, tmp, salt.length, buffer.length);
        buffer = sha1.digest(tmp);
        tmp = new byte[salt.length + buffer.length];
        System.arraycopy(salt,0, tmp, 0, salt.length);
		System.arraycopy(buffer,0, tmp, salt.length, buffer.length);
		return Byte2HexString(tmp);
	}
	public static String ComputeResponse(int fid,String nonce,String cnonce,String domain,String passwordDigest) throws UnsupportedEncodingException, NoSuchAlgorithmException
	{
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
		byte[] begin = (String.valueOf(fid) +  ":" + domain + ":").getBytes("UTF8");
		byte[] end = HexString2Bytes(passwordDigest.substring(8));
		byte[] tmp = new byte[begin.length + end.length];
		System.arraycopy(begin, 0, tmp, 0, begin.length);
		System.arraycopy(end, 0, tmp, begin.length, end.length);
		byte[] key = sha1.digest(tmp);
		begin = key;
		end = (":" + nonce + ":" + cnonce).getBytes("UTF8");
		tmp = new byte[begin.length + end.length];
		System.arraycopy(begin, 0, tmp, 0, begin.length);
		System.arraycopy(end, 0, tmp, begin.length, end.length);
		byte[] h1 = md5.digest(tmp);
		byte[] h2 = md5.digest(("REGISTER:" + String.valueOf(fid)).getBytes("UTF8"));
		
		return Byte2HexString(md5.digest((Byte2HexString(h1) + ":" + nonce + ":" + Byte2HexString(h2)).getBytes("UTF8")));
	}
	public static byte[] toByteArray(int iSource) { 
		byte[] tmp = new byte[4]; 
		for (int i = 0; i < 4; i++) { 
			tmp[i] = (byte)( iSource>> 8*i & 0xFF ); 
		} 
		return tmp; 
	} 

	public static String CreateNonce()
	{
		Random random = new Random();
		int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;
        num1 = random.nextInt();
        num2 = random.nextInt();
        num3 = random.nextInt();
        num4 = random.nextInt();
        if ((num1 >> 0x18) < 0x10)
        {
            num1 += 0x10000000;
        }
        if ((num2 >> 0x18) < 0x10)
        {
            num2 += 0x10000000;
        }
        if ((num3 >> 0x18) < 0x10)
        {
            num3 += 0x10000000;
        }
        if ((num4 >> 0x18) < 0x10)
        {
            num4 += 0x10000000;
        }
        String result = "";
        result += Integer.toHexString(num1);
        result += Integer.toHexString(num2);
        result += Integer.toHexString(num3);
        result += Integer.toHexString(num4);
        return result.toUpperCase();
	}
}
